import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
# 作业1：
# url：https://www.lntcm.com.cn/
# 用例1：
# 搜索骨科—>点击搜索—>点击出来的第一个链接—>断言文本内容
base_url = "https://www.lntcm.com.cn/"
class TestDemo2:
    def setup_method(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    def teardown_method(self):
        sleep(10)
        self.driver.quit()      # 退出浏览器

    def test_guke(self):
        self.driver.get(base_url)
        # 输入文本内容
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.ID, 'showkeycode256695'))).send_keys('骨科')
        sleep(2)

        # 点击搜索按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'sst'))).click()
        sleep(2)

        # 点击出来的第一个链接
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div/div[2]/div[2]/ul/li[1]/a'))).click()
        sleep(2)

        # 断言
        text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/div/div/div[2]/form/h1'))).text
        assert text == '【喜讯】骨一科青年医师徐文嵩在省级骨科赛事中斩获科普组一等奖'
    
    def test_baidu(self):
        self.driver.get('https://www.baidu.com/')
        # 输入内容
        self.driver.find_element(By.ID, 'chat-textarea').send_keys('刘超班长')
        sleep(2)
        # 点击搜索按钮
        self.driver.find_element(By.ID, 'chat-submit-button').click()
        sleep(2)
        # 点击第二个链接
        self.driver.find_element(By.XPATH, '//*[@id="2"]/div/div/h3/a').click()

        # 切换句柄
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])
        # 断言当前页面，是否进入正确，看文章内容
        sleep(2)
        text = self.driver.find_element(By.CLASS_NAME, 'lemmaTitle_mNKG_').text
        assert text == '刘超'
        # 点击：文章中的东北野战军链接
        # self.driver.find_elements(By.CLASS_NAME,'innerLink_dFNtB')[0].click()
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'innerLink_dFNtB')))[
            0].click()

        sleep(2)
        # 切换句柄
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])
        # 断言：中国人民解放军第四野战军，是否正确
        text = self.driver.find_element(By.CLASS_NAME, 'lemmaTitle_mNKG_').text
        assert text == '中国人民解放军第四野战军'
        # 上方词条清空，输入：刘能，点击进入词条
        inputs = self.driver.find_elements(By.CLASS_NAME, 'searchInput')[0]
        inputs.clear()  # 清空文本内容
        sleep(2)
        # 再去输入
        inputs.send_keys('刘能')
        sleep(2)
        # 点击词条搜索
        self.driver.find_elements(By.CLASS_NAME, 'lemmaBtn')[0].click()
        sleep(3)
        # 断言当前页面，是否进入正确，分文章内容
        text = self.driver.find_element(By.CLASS_NAME, 'lemmaTitle_mNKG_').text
        assert text == '刘能'
        # 点击文章中的：乡村爱情
        self.driver.find_element(By.XPATH,
                             '//*[@id="J-lemma-main-wrapper"]/div[2]/div/div[1]/div/div[2]/div[1]/span[2]/a').click()
        # 切换句柄
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])
        sleep(2)
        # 断言
        text = self.driver.find_element(By.CLASS_NAME, 'lemmaTitle_mNKG_').text
        assert text == '乡村爱情'
        # 10、点击导演：张慧中
        self.driver.find_element(By.XPATH,
                             '//*[@id="J-lemma-main-wrapper"]/div[1]/div[2]/div/div[1]/div[1]/div[2]/div[1]/span[15]/a').click()
        sleep(2)
        # 切换句柄
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])
        # 点击文章中的：法库县
        text = self.driver.find_element(By.XPATH,
                                    '//*[@id="J-lemma-main-wrapper"]/div[2]/div/div[1]/div/div[3]/div[1]/span[4]/a').text
        assert text == '法库县'
        text = self.driver.find_element(By.CLASS_NAME, 'lemmaTitle_mNKG_').text
        assert text == '张惠中'
        # 获取页面title
        title = self.driver.title
        assert title == '张惠中（中国内地男导演、辽宁省电视剧制作中心一级导演）_百度百科'

        # 加上等待
        sleep(5)





